<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 创建类
        class Star {
            constructor(uname, age) {
                this.uname = uname;
                this.age = age;
            }
            sing(name) {
                console.log(`${this.uname}唱了一首《${name}》`);
            }
        }
        // 创建对象
        const ldh = new Star('刘德华', 18);
        const zxy = new Star('张学友');
        console.log(ldh);
        ldh.sing('忘情水');
        console.log(zxy);
        zxy.sing('一千个伤心的理由');
        /* for (const key in zxy) {
            const element = zxy[key];
            console.log(element);
        } */

        // 继承
        console.log('********* 继承 *********');
        class Father {
            constructor(money) {
                this.money = money;
            }
            showMoney() {
                console.log(`我有${this.money}块钱。`);
            }
        }
        class Son extends Father {
            constructor(money) {
                super(money);
            }

            showMyMoney() {
                console.log(`showMyMoney: 我有${this.money}块钱。`);
            }
        }
        const son = new Son(200);
        console.log(son);
        son.showMoney();
        son.showMyMoney();

        /* ************* ES6之前写法 **************** */
        console.log('************* ES6之前写法 ****************');
        // 方式一：new关键字
        const cyr = new Object();
        cyr.uname = '崔永元';
        cyr.age = '45';
        cyr.show = function() {
            console.log(`${this.uname}主持实话实说`);
        }
        cyr.show();

        // 方式二：字面量
        const ly = {
            uname: '李咏',
            age: 56,
            sing: function(name) {
                console.log(`${this.uname}唱了一首《${name}》`);
            }
        }
        console.log(ly);
        ly.sing('听妈妈的话');

        // 方式三：构造函数
        function Player(uname, age) {
            this.uname = uname;
            this.age = age;
        }
        Player.type = '电影演员';
        /* Player.prototype.sing = function(name) {
            console.log(`${this.uname}唱了一首《${name}》`);
        }; */
        /* Player.prototype = {
            constructor: Player,
            sing: function(name) {
                console.log(`${this.uname}唱了一首《${name}》`);
            },
            movie: function(name) {
                console.log(`${this.uname}演了一场名叫《${name}》的电影`);
            },
            sign: 'sign'
        }; */
        console.log(Player.prototype);
        const o = {
            constructor: Player,
            sing: function(name) {
                console.log(`${this.uname}唱了一首《${name}》`);
            },
            movie: function(name) {
                console.log(`${this.uname}演了一场名叫《${name}》的电影`);
            },
            sign: 'sign'
        };
        Player.prototype = o;
        console.log(Player.prototype);
        const zxc = new Player('周星驰', 34);
        console.log(zxc);
        console.log(Player.type);
        console.log(zxc.type); // undefined
        console.log(Player.sign); // undefined
        console.log(zxc.sign);
        console.log(zxc.__proto__);
        console.log(zxc.__proto__ === Player.prototype); // true
        console.log(zxc.__proto__ === o); // true
        console.log(Player.prototype === Player.prototype); // true
        console.log(Player.prototype === o); // true
        zxc.sing('少林足球');
        zxc.movie('喜剧之王');

        /* for (const key in zxc) {
            if (Object.hasOwnProperty.call(zxc, key)) {
                const element = zxc[key];
                console.log(element);
            }
        } */
    </script>
</body>

</html>